package icu.wdtt.Utils;

import icu.wdtt.enumModel.HttpRequestMessage;
import icu.wdtt.model.Result;
import org.springframework.stereotype.Component;

@Component
public class ResultUtils {
    /**
     * 当成功时
     * @param msg 消息的提示信息
     * @param data 可传递数据回去，
     *              当传递的是一个集合之类的，请传两个值，
     *              第一个值为集合，第二个值为集合的长度
     * @return
     */
    public Result success(String msg,int statusCode,Object ...data){
        Result result = new Result();
        result.setMsg(msg);
        result.setStatus(statusCode);
        if(data != null && data.length > 0){
            result.setData(data[0]);
            if(data.length > 1){
                result.setCodeSize((Integer)data[1]);
            }else{
                result.setCodeSize(1);
            }
        }else{
            result.setData(null);
        }
        return result;
    }

    /**
     * 使用枚举类httpRequestMessage来统一管理成功信息的返回
     * @param httpRequestMessage
     * @param data
     * @return
     */
    public Result success(HttpRequestMessage httpRequestMessage,Object ...data){
        return this.success(httpRequestMessage.getMessage(),httpRequestMessage.getStatusCode(),data);
    }



    /**
     * 当失败时设置
     * @param msg
     * @return
     */
    public Result fail(String msg,int StatusCode){
        Result result = new Result();
        result.setData(null);
        result.setMsg(msg);
        result.setStatus(StatusCode);
        return result;
    }

    /**
     * 使用枚举类httpRequestMessage来统一管理失败信息的返回
     * @param httpRequestMessage
     * @return
     */
    public Result fail(HttpRequestMessage httpRequestMessage){
        return this.fail(httpRequestMessage.getMessage(),httpRequestMessage.getStatusCode());
    }
}
